<?php
/**
 * QFrameWork
 *
 * @author: quyang
 * @date: 2010-1-17
 * @version: 1.0
 */

/**
 * A Simple class of xmlparser
 */
Class Xmlparser{
    // xml编码
	function xml_encode($data, $encoding = 'utf-8', $root = "root")
	{
		$xml = "<?xml version=\"1.0\" encoding=\"" . $encoding . "\"?>\n";
		$xml .= "<{$root}>\n";
		$xml .= self::_data_to_xml($data);
		$xml .= "</{$root}>";
		return $xml;
	}
	
	function _data_to_xml($data)
	{
		if (is_object($data))
		{
			$data = get_object_vars($data);
		}
		$xml = '';
		foreach($data as $key => $val)
		{
			is_numeric($key) && $key = "item id=\"$key\"";
			$xml .= "<$key>";
			if (is_array($val) || is_object($val))
			{
				$xml .= "\n" . self::_data_to_xml($val);
			}
			else 
			{
				$xml .= in_array($key, array('category', 'title', 'memo', 'tag')) ? "<![CDATA[" . $val . "]]>" : $val;
			}
			list($key,) = explode(' ', $key);
			$xml .= "</$key>\n";
		}
		return $xml;
	}
}
?>